home *** CD-ROM | disk | FTP | other *** search
/ Developer CD Series 2000 November: Tool Chest / Dev.CD Nov 00 TC Disk 1.toast / Sample Code / Human Interface Toolbox / NoSound / main.c next >
Encoding:
C/C++ Source or Header  |  2000-09-28  |  4.2 KB  |  159 lines  |  [TEXT/CWIE]

  1. /*
  2.     File:        main.c
  3.  
  4.     Contains:    it downs the sound level at shutdown time and set it back to its original 
  5.                 level at startup time, once the startup sound is executed. 
  6.  
  7.     Written by: Faustino Forcén    
  8.  
  9.     Copyright:    Copyright © 1992-1999 by Apple Computer, Inc., All Rights Reserved.
  10.  
  11.                 You may incorporate this Apple sample source code into your program(s) without
  12.                 restriction. This Apple sample source code has been provided "AS IS" and the
  13.                 responsibility for its operation is yours. You are not permitted to redistribute
  14.                 this Apple sample source code as "Apple sample source code" after having made
  15.                 changes. If you're going to re-distribute the source, we require that you make
  16.                 it clear in the source that the code was descended from Apple sample source
  17.                 code, but that you've made changes.
  18.  
  19.     Change History (most recent first):
  20.                 8/9/1999    Karl Groethe    Updated for Metrowerks Codewarror Pro 2.1
  21.                 
  22.  
  23. */
  24.  
  25. #include <Finder.h>
  26. #include <shutdown.h>
  27. #include <script.h>
  28. #include <Files.h>
  29. #include <Folders.h>
  30. #include <Errors.h>
  31. #include <Sound.h>
  32. #include <Processes.h>
  33. #include <LowMem.h>
  34.  
  35. int    lee();
  36. pascal void shut_down();
  37.  
  38. int    lee()
  39. {    
  40. int        elValor;
  41. FSSpec    spec;
  42. long    elCount;
  43. long    foundDir;
  44. short    vRef;
  45. OSErr    elError;
  46. Str255    nombredisco="\pNoSound.prefs";
  47. short    refNum;
  48.  
  49.     elError = FindFolder(kOnSystemDisk, 'pref', kDontCreateFolder,&vRef, &foundDir);
  50.             // another bendition from sys 7 or "How to find your way home in a drunky night"
  51.     if (elError == noErr)
  52.     {
  53.         FSMakeFSSpec(vRef, foundDir, nombredisco, &spec);    // the brand new calls from sys 7
  54.         elError = FSpOpenDF(&spec, fsRdWrPerm, &refNum);
  55.         if (elError != noErr)                                 // we failed…
  56.             return(3);                                        // default value
  57.         elCount = 2L;
  58.         FSRead(refNum, &elCount,&elValor);
  59.         FSClose(refNum);
  60.         if(elCount==0)                // file is empty
  61.             elValor=3;                // default value
  62.     }
  63.     else
  64.         elValor=3;
  65.     return(elValor);
  66. }
  67.  
  68. pascal void shut_down()
  69. {
  70.     short    refNum;
  71.     int        valor;    
  72.     SysPPtr    elPuntero;
  73.     FSSpec    spec;
  74.     long    elCount;
  75.     long    foundDir;
  76.     OSErr    elError;
  77.     short    vRef;
  78.     Str255    nombredisco;
  79.     
  80.     
  81.     // we haven´t globals at this time, so…
  82.     nombredisco[0]=13;
  83.     nombredisco[1]='N';
  84.     nombredisco[2]='o';
  85.     nombredisco[3]='S';
  86.     nombredisco[4]='o';
  87.     nombredisco[5]='u';
  88.     nombredisco[6]='n';
  89.     nombredisco[7]='d';
  90.     nombredisco[8]='.';
  91.     nombredisco[9]='p';
  92.     nombredisco[10]='r';
  93.     nombredisco[11]='e';
  94.     nombredisco[12]='f';
  95.     nombredisco[13]='s';
  96.     // yes, is really dirty, but it works
  97.  
  98.     InitUtil();                    // from the PRAM calls
  99.     elPuntero=GetSysPPtr();
  100.     valor = ((elPuntero->volClik & 0x0700) >> 8);
  101.     elError = FindFolder(kOnSystemDisk, 'pref', kDontCreateFolder, &vRef, &foundDir);
  102.     if (elError == noErr)
  103.     {
  104.         FSMakeFSSpec(vRef, foundDir, nombredisco, &spec);
  105.         elError = FSpOpenDF(&spec, fsRdWrPerm, &refNum);
  106.         if (elError == fnfErr)             // this is also your first time, baby?
  107.         {
  108.             FSpCreate(&spec, 'SARA', 'VOL ', smSystemScript);    
  109.                 // yes, my daughter calls SARA, how you guess it?
  110.             FSpOpenDF(&spec, fsRdWrPerm, &refNum);
  111.         }
  112.         elCount = 2L;
  113.         FSWrite(refNum, &elCount, &valor);
  114.         FSClose(refNum);
  115.     }
  116.         // here´s the real stuff, when we change the vol
  117.     elPuntero->volClik = (elPuntero->volClik & 0xf8ff) + (1 << 8);
  118.     WriteParam();
  119. }
  120.  
  121. void main(void)
  122. {
  123.     long size;
  124.     Ptr puntero,otro, temp;
  125.     char buffer[4];
  126.     int    volumen;
  127.     SysPPtr    elPuntero;
  128.     
  129.     volumen=lee();    // we want to get the original volume, stored somewhere
  130.     InitUtil();
  131.     elPuntero=GetSysPPtr();
  132.         // we put the vol to the original value
  133.         // in the worst case (no file, some error) it will be 3 (default value)
  134.     elPuntero->volClik = (elPuntero->volClik & 0xf8ff) + (volumen << 8);
  135.     WriteParam();
  136.         // now tell the Mac that we changed its mind
  137.     SetDefaultOutputVolume(volumen);
  138.     
  139.         // now began the install process
  140.     size = (long) ((Ptr) GetApplLimit() - (Ptr) LMGetApplZone());
  141.     puntero = NewPtrSysClear(size);
  142.     if (puntero) 
  143.     {
  144.         temp = (Ptr) shut_down;
  145.         buffer[0] = *(temp+2);
  146.         buffer[1] = *(temp+3);
  147.         buffer[2] = *(temp+4);
  148.         buffer[3] = *(temp+5);
  149.         BlockMove(LMGetApplZone(),puntero,size);
  150.         otro = (Ptr) (long) puntero + (*(long *)buffer - (long) LMGetApplZone());
  151.         ShutDwnInstall(NewShutDwnProc(otro), sdRestartOrPower + sdOnUnmount + sdOnDrivers);
  152.     }
  153.     ExitToShell();
  154.     // we need to assure that our proc is linked with the app
  155.     shut_down();
  156. }
  157.  
  158. // eof
  159.